﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace RoboRun
{

    public sealed partial class LevelScreen : Page
    {
        public LevelScreen()
        {
            this.InitializeComponent();
            loadPage(obtainScreenWidth(), obtainScreenHeight());
           
        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private float obtainScreenWidth()
        {
            float width = ScreenDimensions.obtainScreenWidth();
            return width;
        }

        private float obtainScreenHeight()
        {
            float height = ScreenDimensions.obtainScreenHeight();
            return height;
        }

        private void loadPage(float screenWidth, float screenHeight)
        {
            levelsTitle.FontSize = screenHeight / 10;
            float rowHeight = (screenHeight - 100) / 3;
            float rowWidth = screenWidth / 3;
            int gap = 80;
            int verticalGap = 30;
            int height = (int) (rowHeight - 30);
            
            //Position level title
            levelsTitle.Margin = new Thickness(0, 50, 0, 0);

            //Back button
            backButton.Margin = new Thickness(50, 50, 0, 0);
            backButton.Width = 300 * screenWidth / 1920;
            backButton.Height = 50 * screenWidth / 1080;
            backButton.Content = "Return to Menu";

            //Position level 1 button
            level1Button.Margin = new Thickness(gap, verticalGap, 0, 0);
            level1Button.Height = rowHeight * 0.65;
            level1Button.Width = rowWidth * 0.65;

            //Level 2 button
            level2Button.Margin = new Thickness(rowWidth + gap, verticalGap, 0, 0);
            level2Button.Height = rowHeight * 0.65;
            level2Button.Width = rowWidth * 0.65;

            //Level 3 button
            level3Button.Margin = new Thickness(( 2 * rowWidth) + gap, verticalGap, 0, 0);
            level3Button.Height = rowHeight * 0.65;
            level3Button.Width = rowWidth * 0.65;

            //Level 4 button
            level4Button.Margin = new Thickness( gap, height  + verticalGap, 0, 0);
            level4Button.Height = rowHeight * 0.65;
            level4Button.Width = rowWidth * 0.65;

            //Level 5 button
            level5Button.Margin = new Thickness(rowWidth + gap, height + verticalGap, 0, 0);
            level5Button.Height = rowHeight * 0.65;
            level5Button.Width = rowWidth * 0.65;

            //Level 6 button
            level6Button.Margin = new Thickness((2 * rowWidth) + gap, height + verticalGap, 0, 0);
            level6Button.Height = rowHeight * 0.65;
            level6Button.Width = rowWidth * 0.65;


            //Level 7 button
            level7Button.Margin = new Thickness(gap, (2 * height) + verticalGap, 0, 0);
            level7Button.Height = rowHeight * 0.65;
            level7Button.Width = rowWidth * 0.65;

            //Level 8 button
            level8Button.Margin = new Thickness(rowWidth + gap, ( 2 * height) + verticalGap, 0, 0);
            level8Button.Height = rowHeight * 0.65;
            level8Button.Width = rowWidth * 0.65;

            //Level 9 button
            level9Button.Margin = new Thickness((2 * rowWidth) + gap, ( 2 * height) + verticalGap, 0, 0);
            level9Button.Height = rowHeight * 0.65;
            level9Button.Width = rowWidth * 0.65;

        }

        private async void level1Button_Click(object sender, RoutedEventArgs e)
        {
            ButtonFlash.buttonFlash(level1ButtonImage, new Uri("ms-appx:///Assets/LevelScreen/Level1_Invert.png", UriKind.Absolute
                  ), new Uri("ms-appx:///Assets/LevelScreen/Level1.png", UriKind.Absolute
                  ));
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            GameplayScreen.GridType = "3x3";
            GameplayScreen.RobotStartingPosition = "3,1";
            GameplayScreen.RobotStartingDirection = "North";
            GameplayScreen.GoalPosition = "1,1";
            String[] arrayOfObstacles = {"2,2","3,2"};
            GameplayScreen.ObstaclePositions = arrayOfObstacles;
            this.Frame.Navigate(typeof(LoadingScreen));
        }

        private void backButton_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainMenu));
        }

        private async void level2Button_Click(object sender, RoutedEventArgs e)
        {
            ButtonFlash.buttonFlash(level2ButtonImage, new Uri("ms-appx:///Assets/LevelScreen/Level2_Invert.png", UriKind.Absolute
                  ), new Uri("ms-appx:///Assets/LevelScreen/Level2.png", UriKind.Absolute
                  ));
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            GameplayScreen.GridType = "3x3";
            GameplayScreen.RobotStartingPosition = "3,2";
            GameplayScreen.RobotStartingDirection = "North";
            GameplayScreen.GoalPosition = "2,3";
            String[] arrayOfObstacles = { "2,1", "1,2" };
            GameplayScreen.ObstaclePositions = arrayOfObstacles;
            this.Frame.Navigate(typeof(LoadingScreen));
        }

        private async void level3Button_Click(object sender, RoutedEventArgs e)
        {
            ButtonFlash.buttonFlash(level3ButtonImage, new Uri("ms-appx:///Assets/LevelScreen/Level3_Invert.png", UriKind.Absolute
                  ), new Uri("ms-appx:///Assets/LevelScreen/Level3.png", UriKind.Absolute
                  ));
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            GameplayScreen.GridType = "3x3";
            GameplayScreen.RobotStartingPosition = "3,1";
            GameplayScreen.RobotStartingDirection = "North";
            GameplayScreen.GoalPosition = "3,3";
            String[] arrayOfObstacles = { "3,2", "2,2" };
            GameplayScreen.ObstaclePositions = arrayOfObstacles;
            
            this.Frame.Navigate(typeof(LoadingScreen));
        }

        private async void level4Button_Click(object sender, RoutedEventArgs e)
        {
            ButtonFlash.buttonFlash(level4ButtonImage, new Uri("ms-appx:///Assets/LevelScreen/Level4_Invert.png", UriKind.Absolute
                  ), new Uri("ms-appx:///Assets/LevelScreen/Level4.png", UriKind.Absolute
                  ));
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            GameplayScreen.GridType = "3x3";
            GameplayScreen.RobotStartingPosition = "3,3";
            GameplayScreen.RobotStartingDirection = "West";
            GameplayScreen.GoalPosition = "1,1";
            String[] arrayOfObstacles = { "1,2", "3,2" };
            GameplayScreen.ObstaclePositions = arrayOfObstacles;
            
            this.Frame.Navigate(typeof(LoadingScreen));
        }

        private async void level5Button_Click(object sender, RoutedEventArgs e)
        {
            ButtonFlash.buttonFlash(level5ButtonImage, new Uri("ms-appx:///Assets/LevelScreen/Level5_Invert.png", UriKind.Absolute
                  ), new Uri("ms-appx:///Assets/LevelScreen/Level5.png", UriKind.Absolute
                  ));
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            GameplayScreen.GridType = "4x4";
            GameplayScreen.RobotStartingPosition = "4,1";
            GameplayScreen.RobotStartingDirection = "North";
            GameplayScreen.GoalPosition = "1,4";
            String[] arrayOfObstacles = { "3,2", "4,3","1,3" };
            GameplayScreen.ObstaclePositions = arrayOfObstacles;
            this.Frame.Navigate(typeof(LoadingScreen));
        }

        private async void level6Button_Click(object sender, RoutedEventArgs e)
        {
            ButtonFlash.buttonFlash(level6ButtonImage, new Uri("ms-appx:///Assets/LevelScreen/Level6_Invert.png", UriKind.Absolute
                  ), new Uri("ms-appx:///Assets/LevelScreen/Level6.png", UriKind.Absolute
                  ));
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            GameplayScreen.GridType = "3x3";
            GameplayScreen.RobotStartingPosition = "1,4";
            GameplayScreen.RobotStartingDirection = "North";
            GameplayScreen.GoalPosition = "4,4";
            String[] arrayOfObstacles = { "1,2", "3,2","4,3","2,4" };
            GameplayScreen.ObstaclePositions = arrayOfObstacles;
            this.Frame.Navigate(typeof(LoadingScreen));
        }

        private async void level7Button_Click(object sender, RoutedEventArgs e)
        {
            ButtonFlash.buttonFlash(level7ButtonImage, new Uri("ms-appx:///Assets/LevelScreen/Level7_Invert.png", UriKind.Absolute
                  ), new Uri("ms-appx:///Assets/LevelScreen/Level7.png", UriKind.Absolute
                  ));
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            GameplayScreen.GridType = "4x4";
            GameplayScreen.RobotStartingPosition = "4,1";
            GameplayScreen.RobotStartingDirection = "North";
            GameplayScreen.GoalPosition = "1,4";
            String[] arrayOfObstacles = { "1,1", "4,2","3,3","2,4" };
            GameplayScreen.ObstaclePositions = arrayOfObstacles;
            this.Frame.Navigate(typeof(LoadingScreen));
        }

        private async void level8Button_Click(object sender, RoutedEventArgs e)
        {
            ButtonFlash.buttonFlash(level8ButtonImage, new Uri("ms-appx:///Assets/LevelScreen/Level8_Invert.png", UriKind.Absolute
                  ), new Uri("ms-appx:///Assets/LevelScreen/Level8.png", UriKind.Absolute
                  ));
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            GameplayScreen.RobotStartingPosition = "3,1";
            GameplayScreen.GridType = "4x4";
            GameplayScreen.RobotStartingDirection = "East";
            GameplayScreen.GoalPosition = "1,4";
            String[] arrayOfObstacles = { "3,2", "4,1","1,3" };
            GameplayScreen.ObstaclePositions = arrayOfObstacles;
            this.Frame.Navigate(typeof(LoadingScreen));
        }

        private async void level9Button_Click(object sender, RoutedEventArgs e)
        {
            ButtonFlash.buttonFlash(level9ButtonImage, new Uri("ms-appx:///Assets/LevelScreen/Level9_Invert.png", UriKind.Absolute
                  ), new Uri("ms-appx:///Assets/LevelScreen/Level9.png", UriKind.Absolute
                  ));
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            GameplayScreen.RobotStartingPosition = "3,2";
            GameplayScreen.GridType = "4x4";
            GameplayScreen.RobotStartingDirection = "West";
            GameplayScreen.GoalPosition = "4,3";
            String[] arrayOfObstacles = { "1,4", "4,1", "4,2","3,3","2,2" };
            GameplayScreen.ObstaclePositions = arrayOfObstacles;
            this.Frame.Navigate(typeof(LoadingScreen));
        }



    }
}
